<?php

require_once 'CommonAction.php';

class DemoGreatingsAction extends CommonAction {
	
	private $_name;
	
	public function __construct() {
		parent::__construct();
		parent::appendObjectList(new ObjectList("model", new DemoGreatingsModel(), ObjectList::_PUBLIC));
	}
	
	public function __request(Parameters $param) {
		parent::__request($param);
		$this->_name	= $this->getParameter("_name")->getValue();
	}
	
	public function __response() {
		if ( parent::getResponse() ) {
			return "demo_hello";
		} else {
			return "demo_main";
		}
	}
	
	public function __execute() {
		
		if ( $this->_name == "" ) {
			parent::appendError( new Error( Error::_RE_EMPTY_VALUE, "Name is empty", Error::_ERROR) );
		}
		
		// Checks only error messages
		if ( parent::countErrors(Error::_ERROR) == 0 ) {
			$_obj = parent::getObjectList("model");
			$_model = $_obj->getValue();
			if ( ! $_model instanceof DemoGreatingsModel ) {
				$_model = new DemoGreatingsModel();
			}
			$_model->setName($this->_name);
			$_obj->setValue($_model);
			parent::appendObjectList($_obj);
			parent::setResponse(CommonAction::_SUCCESS);
		} else {
			parent::setResponse(CommonAction::_FAIL);
		}
	}
}

?>